package cn.wit.summit.chwan.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.IOException;
import java.util.logging.Logger;

import cn.wit.summit.chwan.models.dto.WebAccountDataBean;

/**
 * Created by pengsk on 2018/4/12.
 */

public class JsonMapper {
    private static final JsonMapper ourInstance = new JsonMapper();

    public static JsonMapper getInstance() {
        return ourInstance;
    }

    private JsonMapper() {

    }

    public <T> T fromJson(String json, Class<T> clazz) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        Gson gson = new Gson();
        try {
            return gson.fromJson(json, clazz);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return null;

    }
    /**
     * Object可以是POJO，也可以是Collection或数组。
     * 如果对象为Null, 返回"null".
     * 如果集合为空集合, 返回"[]".
     */
//    public String toJson(Object object) {
//
//        try {
//            return this.writeValueAsString(object);
//        } catch (IOException e) {
//            Logger.getLogger("write to json string error:"+ object.toString()+e.toString());
//            return null;
//        }
//    }

    public String toJson(Object o) {
        Gson gson = new Gson();
        return gson.toJson(o);
    }
}
